/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_components/zhiqim_manager.htm
 *
 * Zhiqim Manager is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
+(function(Z)
{//BEGIN

/**************************************************/
//定义全局的对象，便于所有的页面调用
/**************************************************/
var Zmr = window.Zmr = {};

Zmr.sidebar = function()
{//打开&关闭边导航
    var $sidebar = Z("#sidebar");
    if ($sidebar.isHide())
    {
        $sidebar.show();
        Z("#logo").show();
        Z("#iframenav").css("margin-left", 200);
        Z("#mainbody").css("margin-left", 200);
    }
    else
    {
        $sidebar.hide();
        Z("#logo").hide();
        Z("#iframenav").css("margin-left", 0);
        Z("#mainbody").css("margin-left", 0);
    }
    
    Zmr.calcOprBtnPos();
    Z.ajax("sessionUser", "setSidebar").addParam(!$sidebar.isHide()).execute();
};

Zmr.calcOprBtnPos = function()
{//计算操作位置
    var width = Z(document).clientWidth();
    var marginLeft = Z("#mainbody").cssNum("margin-left") || 0;
    if (width > 1385 + marginLeft){
        Z("#opr-btn").show();Z("#opr-btn2").hide();
    }else{
        Z("#opr-btn").hide();Z("#opr-btn2").show();
    }
}

Zmr.calcMainbodyHeight = function()
{//计算主体高度
    var height = Z(document).clientHeight()-55;//55为topnav的高度
    Z("#container").css("height", height);
    Z("#mainbody").css("height", height-40);//40为iframenav高度
};

Zmr.treeExpand = function(code)
{//树菜单展开和关闭
    var $menu = Z("#menu-"+code);
    var $ico = Z("#ico-"+code);
    
    if (!$menu.isHide())
    {
        $menu.hide();
        if ($ico.hasClass("z-mlastnode"))
            $ico.removeClass("z-mlastnode").addClass("z-plastnode");
        else
            $ico.removeClass("z-mnode").addClass("z-pnode");
    }
    else
    {
        $menu.show();
        if ($ico.hasClass("z-plastnode"))
            $ico.removeClass("z-plastnode").addClass("z-mlastnode");
        else
            $ico.removeClass("z-pnode").addClass("z-mnode");
    }
};

Zmr.addTopMenuClick = function()
{//增加顶级栏目点击事件，先定义两个开关，防止并发时导致数据混乱
    Zmr.doClickTopMenuUp = false;
    Zmr.doClickTopMenuDw = false;
    
    Z(".sidebar p").click(function()
    {
        if (Zmr.doClickTopMenuUp || Zmr.doClickTopMenuDw)
            return;
        
        Zmr.doClickTopMenuUp = true;Zmr.doClickTopMenuDw = true;
        Z(this).next("ul").slideToggle(200, function(){Zmr.doClickTopMenuDw = false;})
                .siblings("ul").slideUp(200, function(){Zmr.doClickTopMenuUp = false;});
    });
};

Zmr.iframenav = function(elem, url)
{//在框架页中增加到框架标签页中
    var nav = parent.Z("li[onclick*='"+url+"']");
    if (nav.length == 0)
    {//再查topnaviframe
        nav = parent.Z("#topnaviframe div[onclick*='"+url+"']");
    }

    if (nav.length == 0)
    {//菜单和标签都没有，则增加到标签中
        var $elem = Z(elem);
        var clickText = $elem.attr("data-text") || $elem.text() || "未命名";
        url = Z.rootPath(parent.Z.cp, url);
        nav = parent.Z("<div onclick='Zin.doClickChildMenu(this, \""+url+"\");' data-text='"+clickText+"'></div>").appendTo("#topnaviframe")
    }
    
    nav.click();
};

/**************************************************/
//定义初始化滑动菜单
/**************************************************/
Z.onload(function()
{//菜单隐藏展开
    Zmr.addTopMenuClick();
});

//END
})(zhiqim);